package cannot.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cannot.pojo.History;
import cannot.pojo.Praise;
import cannot.pojo.Remark;
import cannot.pojo.Tv;
import cannot.pojo.User;
import cannot.pojo.UserDetail;
import cannot.service.HistoryService;
import cannot.service.PraiseService;
import cannot.service.RemarkService;
import cannot.service.TvService;
import cannot.service.UserDetailService;
import cannot.service.UserService;
import cannot.shiro.ShiroUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class TvDetailController {
	@Autowired
	PraiseService praiseService;
	@Autowired
	UserService userSerivce;
	@Autowired
	RemarkService remarkService;
	@Autowired
	TvService tvService;
	@Autowired
	UserDetailService userDetailService;
	@Autowired
	HistoryService historyService;
	
	/**
	 * 加载用户的点赞/收藏行为
	 */
	@RequestMapping("load_user_praise")
	@ResponseBody
	public Object loadUserPraise(HttpSession session){
		User u = (User) session.getAttribute("user");
		if(u==null){
			return "FAIL";
		}
		Long uid = u.getId();
		List<Tv> tvs = tvService.selectTvPraiseByUid(uid);
		return tvs;
	}
	/**
	 * 加载用户的观看记录
	 */
	@RequestMapping("load_user_history")
	@ResponseBody
	public Object loadUserHistory(HttpSession session){
		User u = (User) session.getAttribute("user");
		if(u==null){
			return "FAIL";
		}
		Long uid = u.getId();
		List<History> historys = historyService.getByUid(uid);
		for(History history:historys){
			history.setTv(tvService.selectById(history.getTv_id()));
		}
		return historys;
	}
	
	 /**
     * 点赞
     */
    @RequestMapping("praise")
    @ResponseBody
    public String praise(String type,Integer tvId,HttpSession session){
    	//获取角色id
    	User u = (User) session.getAttribute("user");
    	System.err.println("11111");
    	if(u==null){
    		return "LOGIN";
    	}else{
    		Long uid = u.getId(); 
    		if(type.equals("YES")){
    			//是否存在该记录
    			if(praiseService.selectByTvAndUid(tvId, uid)==null){
    				Praise praise = new Praise();
    				praise.setTv_id(tvId);
    				praise.setUid(uid);
    				praiseService.insertPraise(praise);
    			}else{
    				//存在返回错误
    				return "FAIL";
    			}
    			
    		}else if(type.equals("NO")){
    			Praise praise = praiseService.selectByTvAndUid(tvId, uid);
    			if(praise!=null){
    				praiseService.deletePraise(praise.getId());
    			}else{
    				return "FAIL";
    			}
    		}
    		return "SUCCESS";
    	}
    }
    
    /**
     * 页面加载完成时请求是否已经点赞过
     */
    @RequestMapping("isPraised")
    @ResponseBody
    public String isPraised(Integer tvId){
    	String userName =  ShiroUtil.getUserByPrincipal();
    	if(userName!=null){
    		Long uid = userSerivce.getByName(userName).getId();
        	if(uid!=null&&uid!=0){
        		if(praiseService.selectByTvAndUid(tvId, uid)!=null){
        			return "SUCCESS";
        		}
        	}
    	}
    	return "FAIL";
    }
    /**
     * 加载评论
     */
    @RequestMapping("listRemark")
    @ResponseBody
    public Object listRemark(Integer tvId,@RequestParam(value = "start", defaultValue = "0") int start){
    	PageHelper.startPage(start,6,"id desc");
    	//根据id获取评论列表,倒序排列
    	List<Remark> remarks = remarkService.listRemarkByTvId(tvId);
    		//加载用户信息
    	for(Remark remark:remarks){
    		User u = userSerivce.get(remark.getUid());
    		UserDetail userDetail = userDetailService.getByForeginKey(u.getId());
    		u.setUserDetail(userDetail);
    		u.setId(null);
    		u.setPassword(null);
    		u.setSalt(null);
    		remark.setUser(u);
    	}
    	//分页
    	 PageInfo<Remark> page = new PageInfo<>(remarks);
    	 return page;
    }
    /**
     * 提交评论
     */
    @RequestMapping("submitRemark")
    @Transactional(rollbackFor = { Exception.class })
    @ResponseBody
    public String submitRemark(Integer tvId,String content,Double grades){
    	//获取用户id
    	String userName =  ShiroUtil.getUserByPrincipal();
    	if(userName==null){
    		return "LOGIN";
    	}else{
    		Long uid = userSerivce.getByName(userName).getId();
    		//增加评论与评分
    		Remark remark = new Remark();
    		remark.setContent(content);
    		remark.setUid(uid);
    		remark.setTv_id(tvId);
    		remark.setGrades(grades);
    		remarkService.insertRemark(remark);
    		//计算该影片评分平均值，更新到影片中
    		Double grades1 = remarkService.avgTv(tvId);
    		Tv tv = tvService.selectById(tvId);
    		tv.setGrades(grades1);
    		tvService.updateTv(tv);
    		return "SUCCESS";
    	}
    }
}
